var express = require("express");
var bodyParser = require("body-parser");
var ejs = require('ejs');
var path = require('path');

var app = express(),
	tweets = [];

app.engine('.ejs', ejs.__express);	
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res) {
	var title = "Tweet",
		header = "Welcome to Tweet";
	res.render('index', {
		'title': title,
		'header': header,
		locals: {
			'title': title,
			'header': header,
			'tweets': tweets,
			stylesheets: ['/public/stylesheets/style.css', '/public/stylesheets/bootstap.min.css']
		}
	});
})

app.listen(8000);

app.post('/send', bodyParser(), function(req, res) {
	var title = "Tweet-success",
		header = "Tweet received";

	if (req.body && req.body.tweet) {
		tweets.push(req.body.tweet);
		res.render('success', {
			"title": title,
			"header": header,
			"status": "ok", 
			"message": "Tweet received",
			"tweet": req.body.tweet
		});
	} else {
		res.render('fail', {
			"title": title,
			"header": header,
			"status": "nok", 
			"message": "No Tweet received"
		});
	}
})

app.get('/tweets', function(req, res) {
	res.send(tweets);
})